/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package capanegocio;

import CapaSecretaria.cliente;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Coco
 */
public class User {
    conectate con;
    private String _rut;
    private String _user;
    private String _pass;
  
    public User(){
        con=new conectate();
        _rut="";
        _user="";
        _pass="";
    }
public User verificarUsuario(String user, String clave){
        User u=null;
        Connection cn=null;
        PreparedStatement pr=null;
        ResultSet rs=null;
        try{
            conectate c=new conectate();
            cn=c.getConnection();
            String sql="SELECT * FROM personal WHERE Nombre_Usuario=? AND Clave_Usuario=?";
            pr=cn.prepareStatement(sql);
            pr.setString(2, clave);
            pr.setString(1, user);
            rs=pr.executeQuery();
            while(rs.next()){
                u=new User();
                u.setRut(rs.getString("Rut_Personal"));
                u.setUser(rs.getString("Nombre_Usuario"));
                break;
            }
        }catch(SQLException e){
            JOptionPane.showMessageDialog(null, e);
            u=null;
        }finally{
            try{
                rs.close();
                pr.close();
                rs.close();
            }catch(SQLException e){
                JOptionPane.showMessageDialog(null, e);
            }
        }
        return u;
    }

    public String getRut() {
        return _rut;
    }

    public String getUser() {
        return _user;
    }

    public void setRut(String _rut) {
        this._rut = _rut;
    }

    public void setUser(String _user) {
        this._user = _user;
    }

    public void setPass(String _pass) {
        this._pass = _pass;
    }

    public String getPass() {
        return _pass;
    }
    public void newUser(int Cod,String user,String pass){
       try {            
           PreparedStatement pstm = con.getConnection().prepareStatement("insert into "+
                    "personal(Rut_Personal,Nombre_Usuario,Clave_Usuario) "+
                    "values(?,?,?)");
            pstm.setInt(1, Cod);
            pstm.setString(2, user);
            pstm.setString(3,pass);                        
            pstm.execute();
            pstm.close();
         }catch(SQLException e){
          System.out.println(e);
      }
   }
     public void updateuser(int Cod,String user,String pass){
       try {            
            PreparedStatement pstm = con.getConnection().prepareStatement("update personal " +
            " set Rut_Personal = ? ," +                    
            "Nombre_usuario = ? ,"  +  
            "Clave_Usuario = ? " +
            "where Codigo_Personal = ?");            
            pstm.setInt(1, Cod);                        
            pstm.setString(2, user);
            pstm.setString(3, pass);
            pstm.setString(4, String.valueOf(Cod));
            pstm.execute();
            pstm.close();            
         }catch(SQLException e){
         System.out.println(e);
      }
   }
      public void deleteUser(int cod){  
            try {                
                PreparedStatement pstm = con.getConnection().prepareStatement("delete from personal where Rut_Personal = ?");            
                pstm.setInt(1, cod);
                pstm.execute();
                pstm.close();            
            }catch(SQLException e){
            System.out.println(e);
            }            
   }

    public int getCargo(String cargo) throws SQLException {
        int cargoas;
        Connection c=DriverManager.getConnection("jdbc:mysql://localhost/agua_azul","root","");
        Statement sent=c.createStatement();
        ResultSet rs= sent.executeQuery("Select Rut_Personal from personal where Nombre_usuario = '"+cargo+"'");
        rs.next();
        cargoas=Integer.parseInt(rs.getString("Rut_Personal"));
           
        return cargoas;
    }
    
    public Object[][] getDatos() {
      int registros = 0;
      //obtenemos la cantidad de registros existentes en la tabla
      try{         
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT count(1) as total FROM personal");
         ResultSet res = pstm.executeQuery();
         res.next();
         registros = res.getInt("total");
         res.close();
      }catch(SQLException e){
         System.out.println(e);
      }
      
    Object[][] data = new String[registros][3];  
    //realizamos la consulta sql y llenamos los datos en "Object"
      try{    
         PreparedStatement pstm = con.getConnection().prepareStatement("SELECT " +
            " Rut_Personal, Nombre_Usuario, Clave_Usuario " +
            " FROM personal" +
            " ORDER BY Rut_Personal ");
         ResultSet res = pstm.executeQuery();
         int i = 0;
         while(res.next()){
            String estCodigo = res.getString("Rut_Personal");
            String estcod=res.getString("Nombre_Usuario");
            String estNombre = res.getString("Clave_Usuario");
            data[i][0] = estCodigo;            
            data[i][1] = estcod;            
            data[i][2] = estNombre;
            i++;
         }
         res.close();
          }catch(SQLException e){
         System.out.println(e);
    }
            return data;
    }

    
    
}
